package com.zhongxi.guoyuan.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.http.OkHttpClientManager;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.ad.AdData;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.view.AdActivity;
import com.zhongxi.guoyuan.view.PlayActivity;
import com.zhongxi.guoyuan.view.beautifulStore.ProjectDetailActivity;
import com.zhongxi.guoyuan.view.beautifulStore.StoreDetailActivity;
import com.zhongxi.guoyuan.view.coinShopping.CoinGoodsActivity;
import com.zhongxi.guoyuan.view.coinShopping.CoinShoppingDetailActivity;
import com.zhongxi.guoyuan.view.rewardCoin.RewardCoinDetailActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/14.
 */

public class ImageBannerAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<AdData> imgBannerList;

    public ImageBannerAdapter(Context context, List<AdData> bannerList) {
        this.mContext = context;
        this.imgBannerList = bannerList;
        this.mInflater = LayoutInflater.from(context);
    }

    public void replaceAll(List<AdData> bannerList) {
        if (bannerList == null)
            return;
        if (imgBannerList == null){
            imgBannerList = null;
            imgBannerList = new ArrayList<AdData>(bannerList);
        }else {
            this.imgBannerList.clear();
            imgBannerList.addAll(bannerList);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return imgBannerList != null ? Integer.MAX_VALUE : 0;
    }

    /**
     * 实际数据Size
     *
     * @return
     */
    public int size() {
        return imgBannerList != null ? imgBannerList.size() : 0;
    }

    @Override
    public String getItem(int arg0) {
        return imgBannerList != null ? imgBannerList.get(arg0 % size()).getImage() : null;
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(final int arg0, View arg1, ViewGroup arg2) {
        try {
            ViewHolder holder = null;
            if (arg1 == null) {
                holder = new ViewHolder();
                arg1 = mInflater.inflate(R.layout.adapter_image_item, arg2, false);
                holder.ivImg = (ImageView) arg1.findViewById(R.id.ivImg);
                arg1.setTag(holder);
            }else{
                holder = (ViewHolder) arg1.getTag();
            }
            final String mVpInfo = getItem(arg0);
            if (mVpInfo != null && !TextUtils.isEmpty(mVpInfo)) {
                Glide.with(mContext).load(Url.IMG_URL + mVpInfo).placeholder(R.mipmap.iv_flow).fitCenter().into(holder.ivImg);
            }
            holder.ivImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    try {
                        OkHttpClientManager.getAsyn(Url.CLICK_ADS + MyApplication.getInstance().getCheck(false) + "&adsid=" + imgBannerList.get(arg0 % imgBannerList.size()).getId(), new OkHttpClientManager.ResultCallback<String>() {
                            @Override
                            public void onError(Request request, Exception e) {
                            }

                            @Override
                            public void onResponse(String response) {
                                Gson gson = new Gson();
                                try {
                                    BaseResponse result = gson.fromJson(response, BaseResponse.class);
                                    if (null != result.getSafeobj() && !"".equals(result.getSafeobj())) {
                                        MyApplication.getInstance().setSpValue(result.getSafeobj());
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                        if (0 == imgBannerList.get(arg0 % size()).getIsvideo()) {
                            if (null != imgBannerList.get(arg0 % size()).getUrl() && !"".equals(imgBannerList.get(arg0 % size()).getUrl())) {
                                Uri uri = Uri.parse(imgBannerList.get(arg0 % size()).getUrl());
                                String protocol = uri.getScheme();
                                String server = uri.getAuthority();
                                String path = uri.getPath();
                                String id = uri.getQueryParameter("id");
                                if ("inner".equals(protocol)) {
                                    if ("beauty.item".equals(server)) {
                                        Intent intent = new Intent(mContext, ProjectDetailActivity.class);
                                        intent.putExtra("id", id);
                                        mContext.startActivity(intent);
                                    } else if ("beauty.shop".equals(server)) {
                                        Intent intent = new Intent(mContext, StoreDetailActivity.class);
                                        intent.putExtra("shopid", id);
                                        mContext.startActivity(intent);
                                    } else if ("silvermall.goods".equals(server)) {
                                        Intent intent = new Intent(mContext, CoinGoodsActivity.class);
                                        intent.putExtra("goodsId", id);
                                        mContext.startActivity(intent);
                                    } else if ("silvermall.shop".equals(server)) {
                                        Intent intent = new Intent(mContext, CoinShoppingDetailActivity.class);
                                        intent.putExtra("shopid", id);
                                        mContext.startActivity(intent);
                                    } else if ("welfaremall.goods".equals(server)) {
                            Intent intent = new Intent(mContext, RewardCoinDetailActivity.class);
                            intent.putExtra("goodsId", id);
                            mContext.startActivity(intent);
                                    }
                                } else {
                                    Intent intent = new Intent(mContext, AdActivity.class);
                                    intent.putExtra("url", uri.toString());
                                    mContext.startActivity(intent);
                                }
                            }
                        } else if (imgBannerList.get(arg0 % size()).getIsvideo() == 1 && null != imgBannerList.get(arg0 % size()).getUrl() && !"".equals(imgBannerList.get(arg0 % size()).getUrl())) {
                            Uri uri = Uri.parse(imgBannerList.get(arg0 % size()).getUrl());
                            Intent intent = new Intent(mContext, PlayActivity.class);
                            intent.putExtra("url", uri.toString());
                            mContext.startActivity(intent);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
        return arg1;
    }
    class ViewHolder{
        private ImageView ivImg;
    }

}